package com.dotools.toutiaolibrary;

import android.app.Activity;
import android.util.Log;

import com.bykv.vk.openvk.TTAppDownloadListener;
import com.bykv.vk.openvk.TTRdVideoObject;
import com.bykv.vk.openvk.TTVfConstant;
import com.bykv.vk.openvk.TTVfManager;
import com.bykv.vk.openvk.TTVfNative;
import com.bykv.vk.openvk.VfSlot;

import api.ttreward.Reward_API_TT;

public class TT_RewardVideo extends Reward_API_TT {
    private TTVfNative mTTAdNative;
    private TTRdVideoObject mttRewardVideoAd;
    private boolean mHasShowDownloadActive = false;

    @Override
    public void LoadTTReward(final Activity context, String appid, String adposid, String userId, String RewardName, int RewardAmount, int count, int downloadMode, boolean isDebug, int orientation, boolean isExpress, final TTRewardListener listener) {
        if (appid != null) {
            if (appid.trim().equals("")) {
                return;
            }
        } else {
            return;
        }
        TTVfManager manager = TTManagerHolder.get();
        if (manager != null) {
            mTTAdNative = manager.createVfNative(context.getApplicationContext());
            VfSlot vfSlot;
            if (isExpress) {
                vfSlot = new VfSlot.Builder()
                        .setCodeId(adposid)
                        .setSupportDeepLink(true)
//                    .setRewardName(RewardName) //奖励的名称
//                    .setRewardAmount(RewardAmount)  //奖励的数量
                        .setUserID(userId)//用户id,必传参数
                        .setExpressViewAcceptedSize(500, 500)
                        .setOrientation(orientation) //必填参数，期望视频的播放方向：TTAdConstant.HORIZONTAL 或 TTAdConstant.VERTICAL
                        .build();
            } else {
                vfSlot = new VfSlot.Builder()
                        .setCodeId(adposid)
                        .setSupportDeepLink(true)
//                    .setRewardName(RewardName) //奖励的名称
//                    .setRewardAmount(RewardAmount)  //奖励的数量
                        .setUserID(userId)//用户id,必传参数
                        .setOrientation(orientation) //必填参数，期望视频的播放方向：TTAdConstant.HORIZONTAL 或 TTAdConstant.VERTICAL
                        .build();
            }
            if (downloadMode == 1) {
                vfSlot.setDownloadType(TTVfConstant.DOWNLOAD_TYPE_POPUP);
            }
            //step5:请求广告
            mTTAdNative.loadRdVideoVr(vfSlot, new TTVfNative.RdVideoVfListener() {
                @Override
                public void onError(int code, String message) {
                    listener.onError(code, message);
                }

                //视频广告的素材加载完毕，比如视频url等，在此回调后，可以播放在线视频，网络不好可能出现加载缓冲，影响体验。
                @Override
                public void onRdVideoVrLoad(TTRdVideoObject ttRdVideoObject) {

                    mttRewardVideoAd = ttRdVideoObject;
                    listener.onLoaded();
                    mttRewardVideoAd.setRdVrInteractionListener(new TTRdVideoObject.RdVrInteractionListener() {

                        @Override
                        public void onSkippedVideo() {
                            listener.onSkippedVideo();
                        }

                        @Override
                        public void onShow() {
                            listener.onShow();
                        }

                        @Override
                        public void onVideoBarClick() {
                            listener.onVideoBarClick();
                        }

                        @Override
                        public void onClose() {
                            listener.onClose();
                        }

                        //视频播放完成回调
                        @Override
                        public void onVideoComplete() {
                            listener.onVideoComplete();
                        }

                        @Override
                        public void onVideoError() {
                            listener.onError(400, "onVideoError");
                        }

                        //视频播放完成后，奖励验证回调，rewardVerify：是否有效，rewardAmount：奖励梳理，rewardName：奖励名称
                        @Override
                        public void onRdVerify(boolean b, int i, String s, int i1, String s1) {
                            listener.onRewardVerify(b, i, s);
                        }
                    });
                    mttRewardVideoAd.setDownloadListener(new TTAppDownloadListener() {
                        @Override
                        public void onIdle() {
                            mHasShowDownloadActive = false;
                        }

                        @Override
                        public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) {
                            if (!mHasShowDownloadActive) {
                                mHasShowDownloadActive = true;
                            }
                        }

                        @Override
                        public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) {
                        }

                        @Override
                        public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) {
                        }

                        @Override
                        public void onDownloadFinished(long totalBytes, String fileName, String appName) {
                        }

                        @Override
                        public void onInstalled(String fileName, String appName) {
                        }
                    });
                    mttRewardVideoAd.showRdVideoVr(context);
                }

                //视频广告加载后，视频资源缓存到本地的回调，在此回调后，播放本地视频，流畅不阻塞。
                @Override
                public void onRdVideoCached() {
                    Log.e("Reward", "rewardVideoAd video cached");
                }

                @Override
                public void onRdVideoCached(TTRdVideoObject ttRdVideoObject) {

                }

            });
        }else {
            listener.onError(666, "TTVfManager is null  可能原因sdk未初始化成功");
        }
    }
}
